import Vue from 'vue'
import Router from 'vue-router'
import routerMap from './router.map'

Vue.use(Router)
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const originalReplace = Router.prototype.replace
Router.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err)
}
// 不需要登录拦截的路由配置
const loginIgnore = {
  names: ['404', '403'], //根据路由名称匹配
  paths: ['/login', '/wx/login'], //根据路由fullPath匹配
  /**
   * 判断路由是否包含在该配置中
   * @param route vue-router 的 route 对象
   * @returns {boolean}
   */
  includes(route) {
    return this.names.includes(route.name) || this.paths.includes(route.path)
  }
}

/**
 * 初始化路由实例
 */
function initRouter() {
  return new Router(routerMap)
}

export { loginIgnore, initRouter }
